<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="共用体除非必要，否则我们不推荐使用，枚举的用法比较简单，在本书 19 章的项目实践中有很好的示例，这节课我们先来练习一下结构体的使用。下边这个程序的功能是一个带日期的电子钟，相当于一个简易万年历了，并且加入了按键调时功能。学有余力的同学看到这里，不妨先不看我们提供的代码，自己写写试试。如果能够独立写一个按键可调的万年历程序，单片机可以说基本入门了。如果自己还不能够独立完成这个程序，那么还是老规矩，">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="15.9、C语言复合数据类型">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu105/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="共用体除非必要，否则我们不推荐使用，枚举的用法比较简单，在本书 19 章的项目实践中有很好的示例，这节课我们先来练习一下结构体的使用。下边这个程序的功能是一个带日期的电子钟，相当于一个简易万年历了，并且加入了按键调时功能。学有余力的同学看到这里，不妨先不看我们提供的代码，自己写写试试。如果能够独立写一个按键可调的万年历程序，单片机可以说基本入门了。如果自己还不能够独立完成这个程序，那么还是老规矩，">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-31T13:14:53.363Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="15.9、C语言复合数据类型">
<meta name="twitter:description" content="共用体除非必要，否则我们不推荐使用，枚举的用法比较简单，在本书 19 章的项目实践中有很好的示例，这节课我们先来练习一下结构体的使用。下边这个程序的功能是一个带日期的电子钟，相当于一个简易万年历了，并且加入了按键调时功能。学有余力的同学看到这里，不妨先不看我们提供的代码，自己写写试试。如果能够独立写一个按键可调的万年历程序，单片机可以说基本入门了。如果自己还不能够独立完成这个程序，那么还是老规矩，">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu105/">





  <title>15.9、C语言复合数据类型 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu105/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">15.9、C语言复合数据类型</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:41:05+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>共用体除非必要，否则我们不推荐使用，枚举的用法比较简单，在本书 19 章的项目实践中有很好的示例，这节课我们先来练习一下结构体的使用。下边这个程序的功能是一个带日期的电子钟，相当于一个简易万年历了，并且加入了按键调时功能。学有余力的同学看到这里，不妨先不看我们提供的代码，自己写写试试。如果能够独立写一个按键可调的万年历程序，单片机可以说基本入门了。如果自己还不能够独立完成这个程序，那么还是老规矩，先抄并且理解，而后自己独立默写出来，并且要边默写边理解。</p>
<p>本例直接忽略了星期这项内容，通过上、下、左、右、回车、ESC 这 6 个按键可以调整时间。这也是一个具有综合练习性质的实例，虽然在功能实现上没有多少难度，但要进行的操作却比较多而且烦琐，同学们可以从中体会到把繁杂的功能实现分解为一步步函数操作的必要性以及方便灵活性。简单说一下这个程序的几个要点，方便大家阅读理解程序。</p>
<ol>
<li>把 DS1302 的底层操作封装为一个 DS1302.c 文件，对上层应用提供基本的实时时间的操作接口，这个文件也是我们的又一个功能模块了，我们的积累也越来越多了。</li>
<li>定义一个结构体类型 sTime 用来封装日期时间的各个元素，又用该结构体定义了一个时间缓冲区变量 bufTime 来暂存从 DS1302 读出的时间和设置时间时的设定值。需要注意的是在其它文件中要使用这个结构体变量时，必须首先再声明一次 sTime 类型；</li>
<li>定义一个变量 setIndex 来控制当前是否处于设置时间的状态，以及设置时间的哪一位，该值为 0 就表示正常运行，1～12 分别代表可以修改日期时间的 12 个位；</li>
<li>由于这节课的程序功能要进行时间调整，用到了 1602 液晶的光标功能，添加了设置光标的函数，我们要改变哪一位的数字，就在 1602 对应位置上进行光标闪烁，所以 Lcd1602.c在之前文件的基础上添加了两个控制光标的函数；</li>
<li>时间的显示、增减、设置移位等上层功能函数都放在 main.c 中来实现，当按键需要这些函数时则在按键文件中做外部声明，这样做是为了避免一组功能函数分散在不同的文件内而使程序显得凌乱。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br></pre></td><td class="code"><pre><span class="line">/***************************DS1302.c 文件程序源代码*****************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">sbit DS1302_CE = P1^7;</span><br><span class="line">sbit DS1302_CK = P3^5;</span><br><span class="line">sbit DS1302_IO = P3^4;</span><br><span class="line">struct sTime &#123; //日期时间结构体定义</span><br><span class="line">    unsigned int year; //年</span><br><span class="line">    unsigned char mon; //月</span><br><span class="line">    unsigned char day; //日</span><br><span class="line">    unsigned char hour; //时</span><br><span class="line">    unsigned char min; //分</span><br><span class="line">    unsigned char sec; //秒</span><br><span class="line">    unsigned char week; //星期</span><br><span class="line">&#125;;</span><br><span class="line">/* 发送一个字节到 DS1302 通信总线上 */</span><br><span class="line">void DS1302ByteWrite(unsigned char dat)&#123;</span><br><span class="line">    unsigned char mask;</span><br><span class="line">    for (mask=0x01; mask!=0; mask&lt;&lt;=1)&#123; //低位在前，逐位移出</span><br><span class="line">        if ((mask&amp;dat) != 0)&#123; //首先输出该位数据</span><br><span class="line">            DS1302_IO = 1;</span><br><span class="line">        &#125;else&#123;</span><br><span class="line">            DS1302_IO = 0;</span><br><span class="line">        &#125;</span><br><span class="line">        DS1302_CK = 1; //然后拉高时钟</span><br><span class="line">        DS1302_CK = 0; //再拉低时钟，完成一个位的操作</span><br><span class="line">    &#125;</span><br><span class="line">    DS1302_IO = 1; //最后确保释放 IO 引脚</span><br><span class="line">&#125;</span><br><span class="line">/* 由 DS1302 通信总线上读取一个字节 */</span><br><span class="line">unsigned char DS1302ByteRead()&#123;</span><br><span class="line">    unsigned char mask;</span><br><span class="line">    unsigned char dat = 0;</span><br><span class="line">   </span><br><span class="line">    for (mask=0x01; mask!=0; mask&lt;&lt;=1)&#123; //低位在前，逐位读取</span><br><span class="line">        if (DS1302_IO != 0)&#123; //首先读取此时的 IO 引脚，并设置 dat 中的对应位</span><br><span class="line">            dat |= mask;</span><br><span class="line">        &#125;</span><br><span class="line">        DS1302_CK = 1; //然后拉高时钟</span><br><span class="line">        DS1302_CK = 0; //再拉低时钟，完成一个位的操作</span><br><span class="line">    &#125;</span><br><span class="line">    return dat; //最后返回读到的字节数据</span><br><span class="line">&#125;</span><br><span class="line">/* 用单次写操作向某一寄存器写入一个字节，reg-寄存器地址，dat-待写入字节 */</span><br><span class="line">void DS1302SingleWrite(unsigned char reg, unsigned char dat)&#123;</span><br><span class="line">    DS1302_CE = 1; //使能片选信号</span><br><span class="line">    DS1302ByteWrite((reg&lt;&lt;1)|0x80); //发送写寄存器指令</span><br><span class="line">    DS1302ByteWrite(dat); //写入字节数据</span><br><span class="line">    DS1302_CE = 0; //除能片选信号</span><br><span class="line">&#125;</span><br><span class="line">/* 用单次读操作从某一寄存器读取一个字节，reg-寄存器地址，返回值-读到的字节 */</span><br><span class="line">unsigned char DS1302SingleRead(unsigned char reg)&#123;</span><br><span class="line">    unsigned char dat;</span><br><span class="line">    DS1302_CE = 1; //使能片选信号</span><br><span class="line">    DS1302ByteWrite((reg&lt;&lt;1)|0x81); //发送读寄存器指令</span><br><span class="line">    dat = DS1302ByteRead(); //读取字节数据</span><br><span class="line">    DS1302_CE = 0; //除能片选信号</span><br><span class="line">    return dat;</span><br><span class="line">&#125;</span><br><span class="line">/* 用突发模式连续写入 8 个寄存器数据，dat-待写入数据指针 */</span><br><span class="line">void DS1302BurstWrite(unsigned char *dat)&#123;</span><br><span class="line">    unsigned char i;</span><br><span class="line">   </span><br><span class="line">    DS1302_CE = 1;</span><br><span class="line">    DS1302ByteWrite(0xBE); //发送突发写寄存器指令</span><br><span class="line">    for (i=0; i&lt;8; i++)&#123; //连续写入 8 字节数据</span><br><span class="line">        DS1302ByteWrite(dat[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    DS1302_CE = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 用突发模式连续读取 8 个寄存器的数据，dat-读取数据的接收指针 */</span><br><span class="line">void DS1302BurstRead(unsigned char *dat)&#123;</span><br><span class="line">    unsigned char i;</span><br><span class="line">   </span><br><span class="line">    DS1302_CE = 1;</span><br><span class="line">    DS1302ByteWrite(0xBF); //发送突发读寄存器指令</span><br><span class="line">   </span><br><span class="line">    for (i=0; i&lt;8; i++)&#123; //连续读取 8 个字节</span><br><span class="line">        dat[i] = DS1302ByteRead();</span><br><span class="line">    &#125;</span><br><span class="line">    DS1302_CE = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 获取实时时间，即读取 DS1302 当前时间并转换为时间结构体格式 */</span><br><span class="line">void GetRealTime(struct sTime *time)&#123;</span><br><span class="line">    unsigned char buf[8];</span><br><span class="line">   </span><br><span class="line">    DS1302BurstRead(buf);</span><br><span class="line">    time-&gt;year = buf[6] + 0x2000;</span><br><span class="line">    time-&gt;mon = buf[4];</span><br><span class="line">    time-&gt;day = buf[3];</span><br><span class="line">    time-&gt;hour = buf[2];</span><br><span class="line">    time-&gt;min = buf[1];</span><br><span class="line">    time-&gt;sec = buf[0];</span><br><span class="line">    time-&gt;week = buf[5];</span><br><span class="line">&#125;</span><br><span class="line">/* 设定实时时间，时间结构体格式的设定时间转换为数组并写入 DS1302 */</span><br><span class="line">void SetRealTime(struct sTime *time)&#123;</span><br><span class="line">    unsigned char buf[8];</span><br><span class="line">   </span><br><span class="line">    buf[7] = 0;</span><br><span class="line">    buf[6] = time-&gt;year;</span><br><span class="line">    buf[5] = time-&gt;week;</span><br><span class="line">    buf[4] = time-&gt;mon;</span><br><span class="line">    buf[3] = time-&gt;day;</span><br><span class="line">    buf[2] = time-&gt;hour;</span><br><span class="line">    buf[1] = time-&gt;min;</span><br><span class="line">    buf[0] = time-&gt;sec;</span><br><span class="line">    DS1302BurstWrite(buf);</span><br><span class="line">&#125;</span><br><span class="line">/* DS1302 初始化，如发生掉电则重新设置初始时间 */</span><br><span class="line">void InitDS1302()&#123;</span><br><span class="line">    unsigned char dat;</span><br><span class="line">    struct sTime code InitTime[] = &#123; //2013 年 10 月 8 日 12:30:00 星期二</span><br><span class="line">        0x2013,0x10,0x08, 0x12,0x30,0x00, 0x02</span><br><span class="line">    &#125;;</span><br><span class="line">   </span><br><span class="line">    DS1302_CE = 0; //初始化 DS1302 通信引脚</span><br><span class="line">    DS1302_CK = 0;</span><br><span class="line">    dat = DS1302SingleRead(0); //读取秒寄存器</span><br><span class="line">   </span><br><span class="line">    if ((dat &amp; 0x80) != 0)&#123; //由秒寄存器最高位 CH 的值判断 DS1302 是否已停止</span><br><span class="line">        DS1302SingleWrite(7, 0x00); //撤销写保护以允许写入数据</span><br><span class="line">        SetRealTime(&amp;InitTime); //设置 DS1302 为默认的初始时间</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>DS1302.c 最终向外提供出与具体时钟芯片寄存器位置无关的、由时间结构类型 sTime 作为接口的实时时间的读取和设置函数，如此处理体现了我们前面提到过的层次化编程的思想。应用层可以不关心底层实现细节，底层实现的改变也不会对应用层造成影响，比如说日后你可能需要换一款时钟芯片，而它与 DS1302 的操作和时间寄存器顺序是不同的，那么你需要做的也仅是针对这款新的时钟芯片设计出底层操作函数，最终提供出同样的以 sTime 为接口的操作函数即可，应用层无需做任何的改动。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line">/***************************Lcd1602.c 文件程序源代码*****************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">#define LCD1602_DB P0</span><br><span class="line">sbit LCD1602_RS = P1^0;</span><br><span class="line">sbit LCD1602_RW = P1^1;</span><br><span class="line">sbit LCD1602_E = P1^5;</span><br><span class="line">/* 等待液晶准备好 */</span><br><span class="line">void LcdWaitReady()&#123;</span><br><span class="line">    unsigned char sta;</span><br><span class="line">   </span><br><span class="line">    LCD1602_DB = 0xFF;</span><br><span class="line">    LCD1602_RS = 0;</span><br><span class="line">    LCD1602_RW = 1;</span><br><span class="line">    do &#123;</span><br><span class="line">        LCD1602_E = 1;</span><br><span class="line">        sta = LCD1602_DB; //读取状态字</span><br><span class="line">        LCD1602_E = 0;</span><br><span class="line">    &#125; while (sta &amp; 0x80); //bit7 等于 1 表示液晶正忙，重复检测直到其等于 0 为止</span><br><span class="line">&#125;</span><br><span class="line">/* 向 LCD1602 液晶写入一字节命令，cmd-待写入命令值 */</span><br><span class="line">void LcdWriteCmd(unsigned char cmd)&#123;</span><br><span class="line">    LcdWaitReady();</span><br><span class="line">    LCD1602_RS = 0;</span><br><span class="line">    LCD1602_RW = 0;</span><br><span class="line">    LCD1602_DB = cmd;</span><br><span class="line">    LCD1602_E = 1;</span><br><span class="line">    LCD1602_E = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 向 LCD1602 液晶写入一字节数据，dat-待写入数据值 */</span><br><span class="line">void LcdWriteDat(unsigned char dat)&#123;</span><br><span class="line">    LcdWaitReady();</span><br><span class="line">    LCD1602_RS = 1;</span><br><span class="line">    LCD1602_RW = 0;</span><br><span class="line">    LCD1602_DB = dat;</span><br><span class="line">    LCD1602_E = 1;</span><br><span class="line">    LCD1602_E = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 设置显示 RAM 起始地址，亦即光标位置，(x,y)-对应屏幕上的字符坐标 */</span><br><span class="line">void LcdSetCursor(unsigned char x, unsigned char y)&#123;</span><br><span class="line">    unsigned char addr;</span><br><span class="line">    if (y == 0)&#123; //由输入的屏幕坐标计算显示 RAM 的地址</span><br><span class="line">        addr = 0x00 + x; //第一行字符地址从 0x00 起始</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        addr = 0x40 + x; //第二行字符地址从 0x40 起始</span><br><span class="line">    &#125;</span><br><span class="line">    LcdWriteCmd(addr | 0x80); //设置 RAM 地址</span><br><span class="line">&#125;</span><br><span class="line">/* 在液晶上显示字符串，(x,y)-对应屏幕上的起始坐标，str-字符串指针 */</span><br><span class="line">void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)&#123;</span><br><span class="line">    LcdSetCursor(x, y); //设置起始地址</span><br><span class="line">    while (*str != &apos;\0&apos;)&#123; //连续写入字符串数据，直到检测到结束符</span><br><span class="line">        LcdWriteDat(*str++);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 打开光标的闪烁效果 */</span><br><span class="line">void LcdOpenCursor()&#123;</span><br><span class="line">    LcdWriteCmd(0x0F);</span><br><span class="line">&#125;</span><br><span class="line">/* 关闭光标显示 */</span><br><span class="line">void LcdCloseCursor()&#123;</span><br><span class="line">    LcdWriteCmd(0x0C);</span><br><span class="line">&#125;</span><br><span class="line">/* 初始化 1602 液晶 */</span><br><span class="line">void InitLcd1602()&#123;</span><br><span class="line">    LcdWriteCmd(0x38); //16*2 显示，5*7 点阵，8 位数据接口</span><br><span class="line">    LcdWriteCmd(0x0C); //显示器开，光标关闭</span><br><span class="line">    LcdWriteCmd(0x06); //文字不动，地址自动+1</span><br><span class="line">    LcdWriteCmd(0x01); //清屏</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>为了本例的具体需求，在之前文件的基础上添加两个控制光标效果打开和关闭的函数，虽然函数都很简单，但为了保持程序整体上良好的模块化和层次化，还是应该在液晶驱动文件内以函数的形式提供，而不是由应用层代码直接来调用具体的液晶写命令操作。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br></pre></td><td class="code"><pre><span class="line">/***************************keyboard.c 文件程序源代码****************************/</span><br><span class="line">（此处省略，可参考之前章节的代码）</span><br><span class="line">/*****************************main.c 文件程序源代码******************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">struct sTime &#123; //日期时间结构体定义</span><br><span class="line">    unsigned int year;</span><br><span class="line">    unsigned char mon;</span><br><span class="line">    unsigned char day;</span><br><span class="line">    unsigned char hour;</span><br><span class="line">    unsigned char min;</span><br><span class="line">    unsigned char sec;</span><br><span class="line">    unsigned char week;</span><br><span class="line">&#125;;</span><br><span class="line">bit flag200ms = 1; //200ms 定时标志</span><br><span class="line">struct sTime bufTime; //日期时间缓冲区</span><br><span class="line">unsigned char setIndex = 0; //时间设置索引</span><br><span class="line">unsigned char T0RH = 0; //T0 重载值的高字节</span><br><span class="line">unsigned char T0RL = 0; //T0 重载值的低字节</span><br><span class="line">void ConfigTimer0(unsigned int ms);</span><br><span class="line">void RefreshTimeShow();</span><br><span class="line">extern void InitDS1302();</span><br><span class="line">extern void GetRealTime(struct sTime *time);</span><br><span class="line">extern void SetRealTime(struct sTime *time);</span><br><span class="line">extern void KeyScan();</span><br><span class="line">extern void KeyDriver();</span><br><span class="line">extern void InitLcd1602();</span><br><span class="line">extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);</span><br><span class="line">extern void LcdSetCursor(unsigned char x, unsigned char y);</span><br><span class="line">extern void LcdOpenCursor();</span><br><span class="line">extern void LcdCloseCursor();</span><br><span class="line">void main()&#123;</span><br><span class="line">    unsigned char psec=0xAA; //秒备份，初值 AA 确保首次读取时间后会刷新显示</span><br><span class="line">    EA = 1; //开总中断</span><br><span class="line">    ConfigTimer0(1); //T0 定时 1ms</span><br><span class="line">    InitDS1302(); //初始化实时时钟</span><br><span class="line">    InitLcd1602(); //初始化液晶</span><br><span class="line">   </span><br><span class="line">    //初始化屏幕上固定不变的内容</span><br><span class="line">    LcdShowStr(3, 0, &quot;20 - - &quot;);</span><br><span class="line">    LcdShowStr(4, 1, &quot; : : &quot;);</span><br><span class="line">   </span><br><span class="line">    while (1)&#123;</span><br><span class="line">        KeyDriver(); //调用按键驱动</span><br><span class="line">        if (flag200ms &amp;&amp; (setIndex == 0))&#123; //每隔 200ms 且未处于设置状态时，</span><br><span class="line">            flag200ms = 0;</span><br><span class="line">            GetRealTime(&amp;bufTime); //获取当前时间</span><br><span class="line">            if (psec != bufTime.sec)&#123; //检测到时间有变化时刷新显示</span><br><span class="line">                RefreshTimeShow();</span><br><span class="line">                psec = bufTime.sec; //用当前值更新上次秒数</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 将一个 BCD 码字节显示到屏幕上，(x,y)-屏幕起始坐标，bcd-待显示 BCD 码 */</span><br><span class="line">void ShowBcdByte(unsigned char x, unsigned char y, unsigned char bcd)&#123;</span><br><span class="line">    unsigned char str[4];</span><br><span class="line">   </span><br><span class="line">    str[0] = (bcd &gt;&gt; 4) + &apos;0&apos;;</span><br><span class="line">    str[1] = (bcd&amp;0x0F) + &apos;0&apos;;</span><br><span class="line">    str[2] = &apos;\0&apos;;</span><br><span class="line">    LcdShowStr(x, y, str);</span><br><span class="line">&#125;</span><br><span class="line">/* 刷新日期时间的显示 */</span><br><span class="line">void RefreshTimeShow()&#123;</span><br><span class="line">    ShowBcdByte(5, 0, bufTime.year);</span><br><span class="line">    ShowBcdByte(8, 0, bufTime.mon);</span><br><span class="line">    ShowBcdByte(11, 0, bufTime.day);</span><br><span class="line">    ShowBcdByte(4, 1, bufTime.hour);</span><br><span class="line">    ShowBcdByte(7, 1, bufTime.min);</span><br><span class="line">    ShowBcdByte(10, 1, bufTime.sec);</span><br><span class="line">&#125;</span><br><span class="line">/* 刷新当前设置位的光标指示 */</span><br><span class="line">void RefreshSetShow()&#123;</span><br><span class="line">    switch (setIndex)&#123;</span><br><span class="line">        case 1: LcdSetCursor(5, 0); break;</span><br><span class="line">        case 2: LcdSetCursor(6, 0); break;</span><br><span class="line">        case 3: LcdSetCursor(8, 0); break;</span><br><span class="line">        case 4: LcdSetCursor(9, 0); break;</span><br><span class="line">        case 5: LcdSetCursor(11, 0); break;</span><br><span class="line">        case 6: LcdSetCursor(12, 0); break;</span><br><span class="line">        case 7: LcdSetCursor(4, 1); break;</span><br><span class="line">        case 8: LcdSetCursor(5, 1); break;</span><br><span class="line">        case 9: LcdSetCursor(7, 1); break;</span><br><span class="line">        case 10: LcdSetCursor(8, 1); break;</span><br><span class="line">        case 11: LcdSetCursor(10, 1); break;</span><br><span class="line">        case 12: LcdSetCursor(11, 1); break;</span><br><span class="line">        default: break;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 递增一个 BCD 码的高位 */</span><br><span class="line">unsigned char IncBcdHigh(unsigned char bcd)&#123;</span><br><span class="line">    if ((bcd&amp;0xF0) &lt; 0x90)&#123;</span><br><span class="line">        bcd += 0x10;</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        bcd &amp;= 0x0F;</span><br><span class="line">    &#125;</span><br><span class="line">    return bcd;</span><br><span class="line">&#125;</span><br><span class="line">/* 递增一个 BCD 码的低位 */</span><br><span class="line">unsigned char IncBcdLow(unsigned char bcd)&#123;</span><br><span class="line">    if ((bcd&amp;0x0F) &lt; 0x09)&#123;</span><br><span class="line">        bcd += 0x01;</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        bcd &amp;= 0xF0;</span><br><span class="line">    &#125;</span><br><span class="line">    return bcd;</span><br><span class="line">&#125;</span><br><span class="line">/* 递减一个 BCD 码的高位 */</span><br><span class="line">unsigned char DecBcdHigh(unsigned char bcd)&#123;</span><br><span class="line">    if ((bcd&amp;0xF0) &gt; 0x00)&#123;</span><br><span class="line">        bcd -= 0x10;</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        bcd |= 0x90;</span><br><span class="line">    &#125;</span><br><span class="line">    return bcd;</span><br><span class="line">&#125;</span><br><span class="line">/* 递减一个 BCD 码的低位 */</span><br><span class="line">unsigned char DecBcdLow(unsigned char bcd)&#123;</span><br><span class="line">    if ((bcd&amp;0x0F) &gt; 0x00)&#123;</span><br><span class="line">        bcd -= 0x01;</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        bcd |= 0x09;</span><br><span class="line">    &#125;</span><br><span class="line">    return bcd;</span><br><span class="line">&#125;</span><br><span class="line">/* 递增时间当前设置位的值 */</span><br><span class="line">void IncSetTime()&#123;</span><br><span class="line">    switch (setIndex)&#123;</span><br><span class="line">        case 1: bufTime.year = IncBcdHigh(bufTime.year); break;</span><br><span class="line">        case 2: bufTime.year = IncBcdLow(bufTime.year); break;</span><br><span class="line">        case 3: bufTime.mon = IncBcdHigh(bufTime.mon); break;</span><br><span class="line">        case 4: bufTime.mon = IncBcdLow(bufTime.mon); break;</span><br><span class="line">        case 5: bufTime.day = IncBcdHigh(bufTime.day); break;</span><br><span class="line">        case 6: bufTime.day = IncBcdLow(bufTime.day); break;</span><br><span class="line">        case 7: bufTime.hour = IncBcdHigh(bufTime.hour); break;</span><br><span class="line">        case 8: bufTime.hour = IncBcdLow(bufTime.hour); break;</span><br><span class="line">        case 9: bufTime.min = IncBcdHigh(bufTime.min); break;</span><br><span class="line">        case 10: bufTime.min = IncBcdLow(bufTime.min); break;</span><br><span class="line">        case 11: bufTime.sec = IncBcdHigh(bufTime.sec); break;</span><br><span class="line">        case 12: bufTime.sec = IncBcdLow(bufTime.sec); break;</span><br><span class="line">        default: break;</span><br><span class="line">    &#125;</span><br><span class="line">    RefreshTimeShow();</span><br><span class="line">    RefreshSetShow();</span><br><span class="line">&#125;</span><br><span class="line">/* 递减时间当前设置位的值 */</span><br><span class="line">void DecSetTime()&#123;</span><br><span class="line">    switch (setIndex)&#123;</span><br><span class="line">        case 1: bufTime.year = DecBcdHigh(bufTime.year); break;</span><br><span class="line">        case 2: bufTime.year = DecBcdLow(bufTime.year); break;</span><br><span class="line">        case 3: bufTime.mon = DecBcdHigh(bufTime.mon); break;</span><br><span class="line">        case 4: bufTime.mon = DecBcdLow(bufTime.mon); break;</span><br><span class="line">        case 5: bufTime.day = DecBcdHigh(bufTime.day); break;</span><br><span class="line">        case 6: bufTime.day = DecBcdLow(bufTime.day); break;</span><br><span class="line">        case 7: bufTime.hour = DecBcdHigh(bufTime.hour); break;</span><br><span class="line">        case 8: bufTime.hour = DecBcdLow(bufTime.hour); break;</span><br><span class="line">        case 9: bufTime.min = DecBcdHigh(bufTime.min); break;</span><br><span class="line">        case 10: bufTime.min = DecBcdLow(bufTime.min); break;</span><br><span class="line">        case 11: bufTime.sec = DecBcdHigh(bufTime.sec); break;</span><br><span class="line">        case 12: bufTime.sec = DecBcdLow(bufTime.sec);</span><br><span class="line">        default: break;</span><br><span class="line">    &#125;</span><br><span class="line">    RefreshTimeShow();</span><br><span class="line">    RefreshSetShow();</span><br><span class="line">&#125;</span><br><span class="line">/* 右移时间设置位 */</span><br><span class="line">void RightShiftTimeSet()&#123;</span><br><span class="line">    if (setIndex != 0)&#123;</span><br><span class="line">        if (setIndex &lt; 12)&#123;</span><br><span class="line">            setIndex++;</span><br><span class="line">        &#125;else&#123;</span><br><span class="line">            setIndex = 1;</span><br><span class="line">        &#125;</span><br><span class="line">        RefreshSetShow();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 左移时间设置位 */</span><br><span class="line">void LeftShiftTimeSet()&#123;</span><br><span class="line">    if (setIndex != 0)&#123;</span><br><span class="line">        if (setIndex &gt; 1)&#123;</span><br><span class="line">            setIndex--;</span><br><span class="line">        &#125;else&#123;</span><br><span class="line">            setIndex = 12;</span><br><span class="line">        &#125;</span><br><span class="line">        RefreshSetShow();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 进入时间设置状态 */</span><br><span class="line">void EnterTimeSet()&#123;</span><br><span class="line">    setIndex = 2; //把设置索引设置为 2，即可进入设置状态</span><br><span class="line">    LeftShiftTimeSet(); //再利用现成的左移操作移到位置 1 并完成显示刷新</span><br><span class="line">    LcdOpenCursor(); //打开光标闪烁效果</span><br><span class="line">&#125;</span><br><span class="line">/* 退出时间设置状态，save-是否保存当前设置的时间值 */</span><br><span class="line">void ExitTimeSet(bit save)&#123;</span><br><span class="line">    setIndex = 0; //把设置索引设置为 0，即可退出设置状态</span><br><span class="line">    if (save)&#123; //需保存时即把当前设置时间写入 DS1302</span><br><span class="line">        SetRealTime(&amp;bufTime);</span><br><span class="line">    &#125;</span><br><span class="line">    LcdCloseCursor(); //关闭光标显示</span><br><span class="line">&#125;</span><br><span class="line">/* 按键动作函数，根据键码执行相应的操作，keycode-按键键码 */</span><br><span class="line">void KeyAction(unsigned char keycode)&#123;</span><br><span class="line">    if ((keycode&gt;=&apos;0&apos;) &amp;&amp; (keycode&lt;=&apos;9&apos;))&#123; //本例中不响应字符键</span><br><span class="line">    &#125;else if (keycode == 0x26)&#123; //向上键，递增当前设置位的值</span><br><span class="line">        IncSetTime();</span><br><span class="line">    &#125;else if (keycode == 0x28)&#123; //向下键，递减当前设置位的值</span><br><span class="line">        DecSetTime();</span><br><span class="line">    &#125;else if (keycode == 0x25)&#123; //向左键，向左切换设置位</span><br><span class="line">        LeftShiftTimeSet();</span><br><span class="line">    &#125;else if (keycode == 0x27)&#123; //向右键，向右切换设置位</span><br><span class="line">        RightShiftTimeSet();</span><br><span class="line">    &#125;else if (keycode == 0x0D)&#123; //回车键，进入设置模式/启用当前设置值</span><br><span class="line">        if (setIndex == 0)&#123; //不处于设置状态时，进入设置状态</span><br><span class="line">            EnterTimeSet();</span><br><span class="line">        &#125;else&#123; //已处于设置状态时，保存时间并退出设置状态</span><br><span class="line">            ExitTimeSet(1);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;else if (keycode == 0x1B)&#123; //Esc 键，取消当前设置</span><br><span class="line">        ExitTimeSet(0);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 配置并启动 T0，ms-T0 定时时间 */</span><br><span class="line">void ConfigTimer0(unsigned int ms)&#123;</span><br><span class="line">    unsigned long tmp; //临时变量</span><br><span class="line">    tmp = 11059200 / 12; //定时器计数频率</span><br><span class="line">    tmp = (tmp * ms) / 1000; //计算所需的计数值</span><br><span class="line">    tmp = 65536 - tmp; //计算定时器重载值</span><br><span class="line">    tmp = tmp + 28; //补偿中断响应延时造成的误差</span><br><span class="line">    T0RH = (unsigned char)(tmp&gt;&gt;8); //定时器重载值拆分为高低字节</span><br><span class="line">    T0RL = (unsigned char)tmp;</span><br><span class="line">    TMOD &amp;= 0xF0; //清零 T0 的控制位</span><br><span class="line">    TMOD |= 0x01; //配置 T0 为模式 1</span><br><span class="line">    TH0 = T0RH; //加载 T0 重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    ET0 = 1; //使能 T0 中断</span><br><span class="line">    TR0 = 1; //启动 T0</span><br><span class="line">&#125;</span><br><span class="line">/* T0 中断服务函数，执行按键扫描和 200ms 定时 */</span><br><span class="line">void InterruptTimer0() interrupt 1&#123;</span><br><span class="line">    static unsigned char tmr200ms = 0;</span><br><span class="line">    TH0 = T0RH; //重新加载重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    KeyScan(); //按键扫描</span><br><span class="line">    tmr200ms++;</span><br><span class="line">    if (tmr200ms &gt;= 200)&#123; //定时 200ms</span><br><span class="line">        tmr200ms = 0;</span><br><span class="line">        flag200ms = 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>main.c 主文件，负责所有应用层的功能实现，文件比较长，还是那句话“不难但比较烦琐”，希望对具体问题分析细化能力还不太强的同学们把这个文件多练习几遍，学习一下其中把具体问题逐步细化并一步步实现出来的编程思想，多进行此类练习，锻炼程序思维能力，将来遇到具体项目设计需求的时候，你很快就可以找到方法并实现它们了。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu104/" rel="next" title="15.8、C语言复合数据类型">
                <i class="fa fa-chevron-left"></i> 15.8、C语言复合数据类型
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu106/" rel="prev" title="16.1、红外光的基本原理">
                16.1、红外光的基本原理 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
